package com.masterclient.app.modules.main.contract;

import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.common.config.Config;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.modules.main.beans.ScanDataBean;
import com.masterclient.app.modules.main.beans.ScanWelfareBean;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class ScanPresenter extends ScanContract.Presenter {

    @Override
    public void getScanData(String code) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("code",code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().scanData(body), new
                Consumer<CommonBaseResponse<ScanDataBean>>() {
            @Override
            public void accept(CommonBaseResponse<ScanDataBean> bean) throws Exception {
                if (bean.getCode() == 0){
                    mView.showData(bean.getData());
                }else {
                    mView.loadFail(bean.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.loadFail(o.getMessage());
            }
        }));
    }

    @Override
    public void getDialogData(String code) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("code",code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().scanDialogData(body), new
                Consumer<CommonBaseResponse<ScanWelfareBean>>() {
                    @Override
                    public void accept(CommonBaseResponse<ScanWelfareBean> bean) throws Exception {
                        if (bean.getCode() == 0){
                            mView.showDialogData(bean.getData());
                        }else {
                            mView.loadFail(bean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.loadFail(o.getMessage());
            }
        }));
    }

    @Override
    public void scanSign(String eventId, String customerNo) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("event_id",eventId);
            jsonObject.put("customer_no",customerNo);
            jsonObject.put("token", SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().scanSign(body), (Consumer<CommonBaseResponse>) bean -> {
            if (bean.getCode() == 0){
                mView.scanSignSuccess();
            }else {
                mView.scanSignFail(bean.getMsg());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.scanSignFail(o.getMessage());
            }
        }));
    }
}
